<template>
  <div class="hot-article">
    <h4>热门文章</h4>
    <ul>
      <li v-for="(item, i) in hotArticle" :key="item._id">
        <i>{{ i + 1 }}</i>
        <router-link :to="`/article/${item._id}`">{{ item.title }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "HotArticle",
  data() {
    return {
      allArticle: null,
      hotArticle: [],
    };
  },
  methods: {
    async getHotArticle() {
      let { data } = await this.$axios({
        method: "GET",
        url: "/getData/getArticleList",
      });
      if (data.code) return this.$message.error(data.message);
      this.allArticle = data.data;
      this.allArticle.sort(function (a, b) {
        return b.readingNum - a.readingNum;
      });
      console.log(this.allArticle);
      this.hotArticle.push(...this.allArticle.slice(0, 3));
    },
  },
  created() {
    this.getHotArticle();
  },
};
</script>

<style lang="less" scoped>
.hot-article {
  width: 100%;
  background-color: #fff;
  padding: 20px;
  font-family: "Quicksand";
  box-shadow: 0 0 4px #ddd;
  box-sizing: border-box;
  h4 {
    line-height: 20px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e8e9e7;
    color: #383937;
    font-size: 16px;
    font-weight: bold;
  }
  ul {
    margin-top: 15px;
    li {
      overflow: hidden;
      display: flex;
      height: 40px;
      padding: 0 3px;
      line-height: 40px;
      box-sizing: border-box;
      i {
        width: 22px;
        height: 22px;
        margin-top: 9px;
        margin-right: 15px;
        border-radius: 50%;
        line-height: 22px;
        font-size: 12px;
        font-style: normal;
        color: #fff;
        text-align: center;
        font-weight: 100;
        background-color: #71a598;
      }
      &:nth-child(2) i {
        background-color: #6eb3a3;
      }
      &:nth-child(3) i {
        background-color: #93bbb2;
      }
      a {
        display: block;
        overflow: hidden;
        color: #787977;
        font-size: 14px;
        text-decoration: none;
        white-space: nowrap;
        text-overflow: ellipsis;
        flex: 1;
      }
    }
  }
}
</style>